package com.hifar.test.api.model;

import com.hifar.test.api.constant.OrganizationValidationErrorCode;

import java.util.ArrayList;
import java.util.List;

/**
 * 组织信息验证结果类
 * 封装组织信息数据验证的详细结果
 */
public class OrganizationValidationResult {
    
    private List<OrganizationValidationErrorCode.OrganizationValidationErrorResponse> errors;
    private int totalCount;
    private int validCount;
    private int errorCount;
    
    public OrganizationValidationResult() {
        this.errors = new ArrayList<>();
        this.totalCount = 0;
        this.validCount = 0;
        this.errorCount = 0;
    }
    
    /**
     * 添加错误
     */
    public void addError(OrganizationValidationErrorCode errorCode) {
        this.errors.add(errorCode.createErrorResponse());
        this.errorCount++;
    }
    
    /**
     * 添加带字段信息的错误
     */
    public void addError(OrganizationValidationErrorCode errorCode, int index, String fieldName, Object fieldValue) {
        this.errors.add(errorCode.createErrorResponse(index, fieldName, fieldValue));
        this.errorCount++;
    }
    
    /**
     * 添加错误响应
     */
    public void addError(OrganizationValidationErrorCode.OrganizationValidationErrorResponse errorResponse) {
        this.errors.add(errorResponse);
        this.errorCount++;
    }
    
    /**
     * 标记验证成功
     */
    public void markValid() {
        this.validCount++;
    }
    
    /**
     * 设置总数
     */
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }
    
    /**
     * 设置错误数量
     */
    public void setErrorCount(int errorCount) {
        this.errorCount = errorCount;
    }
    
    /**
     * 检查是否有效
     */
    public boolean isValid() {
        return this.errorCount == 0;
    }
    
    /**
     * 获取错误数量
     */
    public int getErrorCount() {
        return this.errorCount;
    }
    
    /**
     * 获取有效数量
     */
    public int getValidCount() {
        return this.validCount;
    }
    
    /**
     * 获取总数
     */
    public int getTotalCount() {
        return this.totalCount;
    }
    
    /**
     * 获取错误列表
     */
    public List<OrganizationValidationErrorCode.OrganizationValidationErrorResponse> getErrors() {
        return this.errors;
    }
    
    /**
     * 获取成功率
     */
    public double getSuccessRate() {
        if (totalCount == 0) {
            return 0.0;
        }
        return (double) validCount / totalCount * 100;
    }
    
    /**
     * 获取失败率
     */
    public double getFailureRate() {
        if (totalCount == 0) {
            return 0.0;
        }
        return (double) errorCount / totalCount * 100;
    }
    
    /**
     * 检查是否有错误
     */
    public boolean hasErrors() {
        return !errors.isEmpty();
    }
    
    /**
     * 获取错误统计信息
     */
    public String getErrorSummary() {
        if (errors.isEmpty()) {
            return "验证通过，无错误";
        }
        
        StringBuilder summary = new StringBuilder();
        summary.append("验证失败，共发现 ").append(errorCount).append(" 个错误：\n");
        
        for (int i = 0; i < errors.size(); i++) {
            OrganizationValidationErrorCode.OrganizationValidationErrorResponse error = errors.get(i);
            summary.append(i + 1).append(". ").append(error.getMessage());
            if (error.getFieldName() != null) {
                summary.append(" (字段: ").append(error.getFieldName()).append(")");
            }
            if (error.getIndex() != null) {
                summary.append(" (索引: ").append(error.getIndex()).append(")");
            }
            summary.append("\n");
        }
        
        return summary.toString();
    }
    
    @Override
    public String toString() {
        return "OrganizationValidationResult{" +
                "totalCount=" + totalCount +
                ", validCount=" + validCount +
                ", errorCount=" + errorCount +
                ", successRate=" + String.format("%.2f", getSuccessRate()) + "%" +
                ", hasErrors=" + hasErrors() +
                '}';
    }
}
